﻿namespace SharpF2.Components
{
	using System;
	using System.Collections.Generic;
	using System.Collections.Specialized;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Data;
	using Services;

	/// <summary>
	/// Interaction logic for ListViewExtended.Columns
	/// </summary>
	public partial class ListViewExtended<ItemType>
	{
		#region Public Attributes

		public static readonly DependencyProperty ColumnsSourceProperty =
			DependencyProperty.Register("ColumnsSource",
			                            typeof (IEnumerable<ItemMetaData<ItemType>>),
			                            typeof (ListViewExtended<ItemType>),
			                            new FrameworkPropertyMetadata(
			                            	null,
			                            	new PropertyChangedCallback(OnColumnsSourcePropertyChanged)));

		#endregion

		#region Public Properties

		public IEnumerable<ItemMetaData<ItemType>> ColumnsSource
		{
			get { return GetValue(ColumnsSourceProperty) as IEnumerable<ItemMetaData<ItemType>>; }
			set { SetValue(ColumnsSourceProperty, value); }
		}

		#endregion

		#region Private Members

		/// <summary>
		/// Create a column with a specified textBinding and column header columnHeaderText.
		/// </summary>
		/// <param name="columnMetaData">The column MetaData.</param>
		/// <returns>
		/// The created GridViewColumn.
		/// </returns>
		private static GridViewColumn GetColumn(ItemMetaData<ItemType> columnMetaData)
		{
			GridViewColumnExtension<ItemType> gridViewColumn;
			if (String.IsNullOrEmpty(columnMetaData.HeaderBindingName))
			{
				gridViewColumn = new GridViewColumnExtension<ItemType>
				                 	{
				                 		Header = columnMetaData.HeaderName,
				                 		PropertyName = columnMetaData.PropertyName,
				                 		Compare = columnMetaData.Comparer,
				                 		Sortable = columnMetaData.Sortable,
				                 		DisplayMemberBinding = new Binding(columnMetaData.PropertyName),
				                 		Width = double.NaN
				                 	};
			}
			else
			{
				var headerTemplate = new DataTemplate();
				var textBlock = new FrameworkElementFactory(typeof (TextBlock));
				textBlock.SetValue(TextBlock.TextProperty, new LanguageExtension(columnMetaData.HeaderBindingName));
				headerTemplate.VisualTree = textBlock;

				gridViewColumn = new GridViewColumnExtension<ItemType>
				                 	{
				                 		HeaderTemplate = headerTemplate,
				                 		PropertyName = columnMetaData.PropertyName,
				                 		Compare = columnMetaData.Comparer,
				                 		Sortable = columnMetaData.Sortable,
				                 		DisplayMemberBinding = new Binding(columnMetaData.PropertyName),
				                 		Width = double.NaN
				                 	};
			}
			return gridViewColumn;
		}

		private static void OnColumnsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var listViewExtended = d as ListViewExtended<ItemType>;
			if (null == listViewExtended) return;

			var oldCollection = e.OldValue as INotifyCollectionChanged;
			if (null != oldCollection)
				oldCollection.CollectionChanged -= listViewExtended.OnColumnsSourceChanged;

			var newCollection = e.NewValue as INotifyCollectionChanged;
			if (null != newCollection)
				newCollection.CollectionChanged += listViewExtended.OnColumnsSourceChanged;

			listViewExtended.UpdateColumns();
		}

		private void OnColumnsSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			UpdateColumns();
		}

		/// <summary>
		/// Update temporary column from an identifiers collection.
		/// </summary>
		private void UpdateColumns()
		{
			var gridView = View as GridView;
			if (null == gridView) return;

			gridView.Columns.Clear();
			foreach (var itemMetaData in ColumnsSource)
				gridView.Columns.Add(GetColumn(itemMetaData));
		}

		#endregion
	}
}